package com.bw.system.controller;

import cn.hutool.extra.qrcode.QrCodeUtil;
import com.alibaba.fastjson2.JSON;
import com.alibaba.fastjson2.JSONObject;
import com.alipay.easysdk.factory.Factory;
import com.alipay.easysdk.kernel.Config;
import com.alipay.easysdk.payment.facetoface.models.AlipayTradePrecreateResponse;
import com.bw.common.core.domain.R;
import lombok.AllArgsConstructor;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.io.File;

/**
 * @Author：尚志豪
 * @Package：com.bw.system.controller
 * @Project：ruoyiszh
 * @name：AliPayController
 * @Date：2024/12/31 9:20
 */

@RestController
@AllArgsConstructor
public class AliPayController {

    private final Config aliPayConfig;


    @GetMapping("/pay")
    public R pay() throws Exception {

        Factory.setOptions(aliPayConfig);
        // 面对面支付[用户扫商家]
        AlipayTradePrecreateResponse response = Factory.Payment.FaceToFace().preCreate("华为x60", "123456", "8888.88");

        JSONObject httpBodyJsonObject = JSON.parseObject(response.getHttpBody().toString());
        String qrCodeString = httpBodyJsonObject.getJSONObject("alipay_trade_precreate_response").getString("qr_code");

        QrCodeUtil.generate(qrCodeString,400,400,new File("D://pay.jpg"));

        return R.ok(qrCodeString);
    }

    @PostMapping("/notify")
    public R notifyPay(HttpServletRequest request)
    {
        // 支付成功了,获得订单号
        System.out.println("支付成功,一会获得订单号,继续处理");
        return R.ok();
    }
}
